var Util = {
    Cookie: {
        set: function (name, value, expire) {
            var exp = new Date();
            exp.setTime(exp.getTime() + expire * 24 * 60 * 60 * 1000);
            document.cookie = name + "=" + encodeURIComponent(value, "UTF-8") + ";expires=" + exp.toGMTString() + ";domain=passport.jd.com;path=/";
        },
        get: function (key) {
            var cookies = document.cookie ? document.cookie.split('; ') : [];
            for (var i = 0, l = cookies.length; i < l; i++) {
                var parts = cookies[i].split('=');
                var name = parts.shift();
                var cookie = parts.join('=');
                if (key && key === name) {
                    return cookie;
                }
            }
        },
        setALCookie: function () {
            if ($("input[name='chkRememberMe']").prop("checked")) {
                var Days = 3 * 30;
                this.set("alpin", $("#loginname").val(), Days);
            } else {
                var v = this.get("alpin");
                if (v) {
                    this.set("alpin", "", -100);
                }
            }
        }
    },
    Header: {
        getProtocol: function(){
            var parentScheme = '';
            try{
                parentScheme = parent.location.protocol;
            }catch(e){
                parentScheme = LoginConstant.HTTP_SCHEME;
            }
            if(!parentScheme){
                parentScheme = LoginConstant.HTTP_SCHEME;
            }
            return parentScheme;
        }
    }
};

function showMesInfo(msg, type) {
    $('.form>.msg-wrap').empty();
    if (type == 'warn') {
        var info = '<div class="msg-warn"><b></b>' + msg + '</div>';
        $('.form>.msg-wrap').append(info);
    }
    if (type == 'error') {
        var info = '<div class="msg-error"><b></b>' + msg + '</div>';
        $('.form>.msg-wrap').append(info);
    }
}
function assemblyForm(){
    //获取安全控件，并对loginpwd隐藏域进行填充
   
}

function loginSubmit(callback) {
    $('#loginsubmit').text('正在登录...');
    
    var loginUrl = "/index.php";
    //var uuid = $("#uuid").val();
    $.ajax({
        url: loginUrl + "?" + location.search.substring(1) + "&r=" + Math.random()+"&version=2015",
        type: "POST",
        dataType: "text",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data: $("#formlogin").serialize(),
        error: function () {
            showMesInfo("网络超时，请稍后再试","error");
        },
        success: function (result) {
            if (result) {
                var obj = eval(result);
                if (obj.success) {
                    Util.Cookie.setALCookie();
                    var isIE = !-[1,];
                    if (isIE) {
                        var link = document.createElement("a");
                        link.href = obj.success;
                        link.style.display = 'none';
                        document.body.appendChild(link);
                        link.click();
                    } else {
                        window.location = obj.success;
                    }
                    return;
                }

                if (obj.authcode2) {
                	change_captcha($('#captcha'));
                }
               
                if (obj.authcode2) {
                    callback(obj.authcode2,"error",["#authcode"]);
                }
                if (obj.error) {
                    callback(obj.error,"error","");
                }
                
                if (obj.username) {
                    initCountryCode(obj);
                    callback(obj.username,"error",["#loginname"]);
                }
                if (obj.pwd) {
                    callback(obj.pwd,"error",["#nloginpwd"]);
                    clearPwd();
                    change_captcha($('#captcha'));
                }
            }
            $("#loginsubmit").html("登&nbsp;&nbsp;&nbsp;&nbsp;录");
        }
    });
}

function change_captcha(jqObj){
    jqObj.attr('src', 'index.php?app=captcha&' + Math.round(Math.random()*10000));
}

function clearPwd(){
    $("#nloginpwd").val("");
    $("#loginpwd").val("");
    $('#nloginpwd').siblings('.clear-btn').hide();
}